﻿using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Text;

namespace JsiCommon.javaScriptInterop
{
    public static class JsiInterfaceEmiter
    {
        public static T Create<T>(IJsiExpression obj)
        {
            var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(new AssemblyName("tmp" + Guid.NewGuid()), AssemblyBuilderAccess.Run);
            var typeName = typeof(T).FullName;
            if (typeName == null)
                throw new Exception("typeName == null");
            var module = assembly.DefineDynamicModule(typeName);
            var type = module.DefineType(typeName, TypeAttributes.Class, typeof (JsiInterfaceImpl),
                                         new[] {typeof (T)});
            foreach (var methodInfo in typeof(T).GetMethods())
            {
                var method = type.DefineMethod(methodInfo.Name, MethodAttributes.Public);
            }
            return default(T);
        }

        public abstract class JsiInterfaceImpl:IJsiExpression
        {
            public abstract void Compile(StringBuilder builder);
        }
    }
}